package com.tan.util;

import java.math.BigDecimal;
import java.util.Random;

/**
 * 算数运算
 * @Author:
 */
public class NumberUtils {

    /**
     * 计算两数相加或者相加
     * @param dou1
     * @param dou2
     * @return
     */
    public static double add(double dou1, double dou2) {
        BigDecimal big1 = new BigDecimal(String.valueOf(dou1));
        BigDecimal big2 = new BigDecimal(String.valueOf(dou2));
        return big1.add(big2).doubleValue();
    }

    /**
     * 计算两数相乘
     * @param dou1
     * @param dou2
     * @return
     */
    public static double multi(double dou1, double dou2) {
        BigDecimal big1 = new BigDecimal(String.valueOf(dou1));
        BigDecimal big2 = new BigDecimal(String.valueOf(dou2));
        return big1.multiply(big2).doubleValue();
    }

    /**
     * 计算两数相除
     * @param dou1
     * @param dou2
     * @return
     */
    public static double divide(double dou1, double dou2) {
        BigDecimal big1 = new BigDecimal(String.valueOf(dou1));
        BigDecimal big2 = new BigDecimal(String.valueOf(dou2));
        return big1.divide(big2, 2).doubleValue();
    }

    /**
     * 生成指定数内的随机数,不包括0
     * @param bound
     * @return
     */
    public static int randomNum(int bound) {
        if (bound <= 0) {
            bound = 1;
        }
        Random random = new Random();
        return random.nextInt(bound) + 1;
    }

    /**
     * 生成指定数内的随机数,不包括0
     * @param bound
     * @return
     */
    public static long randomNum(long bound) {
        if (bound <= 0) {
            bound = 1;
        }
        int parseInt = Integer.parseInt(bound + "");
        return randomNum(parseInt);
    }
}
